package com.putong35.common.app;


import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.baidu.mobstat.StatService;
import com.putong35.common.BuildConfig;
import com.putong35.common.account.Account;
import com.putong35.common.account.AccountManager;
import com.putong35.common.httprequest.retrofit.converter.GsonConverterFactory;
import com.putong35.common.app.data.UrlConfig;
import com.putong35.common.utils.DimensionUtil;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;


public class BaseApplication extends Application {

    private static Context mAppContext = null;

    @Override
    public void onCreate() {
        super.onCreate();

        String packageName = getPackageName();
        String processName = getProcessName(android.os.Process.myPid());
        // 主进程中初始化， 保证只初始化一次
        if (processName == null || processName.equals(packageName)) {
            mAppContext = this.getApplicationContext();
            DimensionUtil.init(mAppContext, getResources());
            // baidu 统计
            StatService.setAppKey(BuildConfig.BAIDU_APP_KEY);
            StatService.autoTrace(this);
        }
    }


    public static Context getAppContext() {
        return mAppContext;
    }


    public static Retrofit retrofit() {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        Account account = AccountManager.getInstance().getAccount();
        if (account != null) {
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request original = chain.request();
                    Request request = original.newBuilder()
                            .addHeader("token", account.getToken())
                            .method(original.method(), original.body())
                            .build();

                    return chain.proceed(request);
                }
            });
        }

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConfig.URL_PREFIX) // 设置 网络请求 Url
                .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                .client(httpClient.build())
                .build();
        return retrofit;
    }

/*

    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    public static final String DATABASE_NAME = "demoapp.db";

    public static DaoMaster getDaoMaster(Context context) {
        if (mDaoMaster == null) {
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
            mDaoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return mDaoMaster;
    }


    */
/**
 * 取得DaoSession
 *
 * @param context
 * @return
 *//*

    public static DaoSession getDaoSession(Context context) {
        if (mDaoSession == null) {
            if (mDaoMaster == null) {
                mDaoMaster = getDaoMaster(context);
            }
            mDaoSession = mDaoMaster.newSession();
        }
        return mDaoSession;
    }
*/


    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

}
